﻿using B.S.Data.MES.API.Applications.AccountApplication.Command;
using B.S.Data.MES.API.Dto.LoginDTO;
using B.S.Domain.Accounts;
using B.S.Infratrctrue;
using B.S.Requst;
using FluentAssertions.Common;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;

namespace B.S.Data.MES.API.Applications.AccountApplication.CommandHandler
{
    /// <summary>
    /// 实现用户显示查询命令处理器
    /// </summary>
    public class AccountQueryCommandHandler : IRequestHandler<AccountQueryCommand, APIRequst<APIPageing<List<AccountDTO>>>>
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="accountRepository">用户</param>
        /// <param name="factoryClassRepository">工厂</param>
        /// <param name="orcanClassRepository">组织</param>
        public AccountQueryCommandHandler(IBaseRepository<Account> accountRepository,IBaseRepository<FactoryClass> factoryClassRepository,IBaseRepository<OrcanClass> orcanClassRepository)
        {
            AccountRepository = accountRepository;
            FactoryClassRepository = factoryClassRepository;
            OrcanClassRepository = orcanClassRepository;
        }
        /// <summary>
        /// 用户仓储
        /// </summary>
        public IBaseRepository<Account> AccountRepository { get; }
        /// <summary>
        /// 工厂仓储
        /// </summary>
        public IBaseRepository<FactoryClass> FactoryClassRepository { get; }
        /// <summary>
        /// 组织仓储
        /// </summary>
        public IBaseRepository<OrcanClass> OrcanClassRepository { get; }

        public async Task<APIRequst<APIPageing<List<AccountDTO>>>> Handle(AccountQueryCommand request, CancellationToken cancellationToken)
        {
            var listDto=from a in AccountRepository.GetAll()
                        join b in FactoryClassRepository.GetAll() on a.factoryId equals b.FactoryId
                        join c in OrcanClassRepository.GetAll() on a.DeptId equals c.OranId
                        select new AccountDTO
                        {
                          UserId=a.UserId,
                          UserName=a.UserName,
                          UserNickname=a.UserNickname,
                          UserPass=a.UserPass,
                          Iocn=a.Iocn,
                           factoryId=b.FactoryId,
                           FactoryName=b.FactoryName,
                            Sex=a.Sex,
                            dataid=c.OranId,
                            OranCord=c.OranCord,
                            CreateName=a.CreateName,
                            CreateTime=a.CreateTime
                        };
            if(!string.IsNullOrEmpty(request.UserName))
            {
                listDto = listDto.Where(a => a.UserName.Contains(request.UserName));
            }
            if(!string.IsNullOrEmpty(request.UserNickname))
            {
                listDto = listDto.Where(a => a.UserNickname == request.UserNickname);
            }
            if(!string.IsNullOrEmpty(request.CreateName))
            {
                listDto = listDto.Where(a => a.CreateName == request.CreateName);
            }
            if(request.StartTime!=null)
            {
                listDto = listDto.Where(a => a.CreateTime >=request.StartTime);
            }
            if(!string.IsNullOrEmpty(request.EndTime))
            {
                listDto = listDto.Where(a => a.CreateTime <= Convert.ToDateTime(request.EndTime).AddDays(1));
            }
            if(request.RoleId==1)
            {
                listDto = listDto.Where(a=>a.UserId>0);
            }
            ////var str= RedisHelper.LPush("h",listDto.ToList());
            //var str = RedisHelper.LRange("h", 0, -1);
            //var lists = str.Select(a=>System.Text.Json.JsonSerializer.Deserialize<AccountDTO>(a));
            //var str = RedisHelper.LPush("sa", listDto);
            //var s = RedisHelper.LRange<List<AccountDTO>>("sa", 0, 3)[0];

            var totaclCount = listDto.Count();
            var pageCount = (int)Math.Ceiling(totaclCount*1.0/request.pageSize);
            var list = listDto.OrderBy(a => a.UserId).Skip((request.pageIndex-1) * request.pageSize).Take(request.pageSize).ToList();
            return new APIRequst<APIPageing<List<AccountDTO>>>
            {
                Code = RequstCode.成功,
                Msg = "查询成功",
                Data = new APIPageing<List<AccountDTO>>
                {
                    TotalCount = totaclCount,
                    Total = pageCount,
                    Data = list
                }
            };


        }
    }
}
